package org.lmw.mm.app;

import org.lmw.mm.ctrl.CommCtrl;
import org.lmw.mm.ctrl.PostCtrl;
import org.lmw.mm.ctrl.UserCtrl;
import org.lmw.mm.dao.Coment;
import org.lmw.mm.dao.Like_Post;
import org.lmw.mm.dao.Post;
import org.lmw.mm.dao.User;
import org.lmw.mm.dao.Coment_Post;
import org.lmw.mm.interceptor.GlobalInterceptor;
import org.lmw.mm.utils.MyRenderFactory;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;

public class AppConfig extends JFinalConfig {
  public static final int PAGE_SIZE = 20;
  public static final String APPID = "_mimi_";

  @Override
  public void configConstant(Constants me) {
    me.setDevMode(true);
//  loadPropertyFile("local_config.txt");
    loadPropertyFile("paas_config.txt");
    me.setBaseViewPath(getProperty("TPL_ROOT_DIR"));
    me.setMainRenderFactory(new MyRenderFactory(getProperty("TPL_ROOT_DIR")));
  }

  @Override
  public void configRoute(Routes me) {
    me.add("/", CommCtrl.class);
    me.add("/u", UserCtrl.class);
    me.add("/p", PostCtrl.class);
  }

  @Override
  public void configPlugin(Plugins me) {
    C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password").trim());
    me.add(c3p0Plugin);
    ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
    me.add(arp);

    arp.addMapping("mm_post", Post.class);
    arp.addMapping("mm_coment", Coment.class);
    arp.addMapping("mm_user", User.class);
    arp.addMapping("mm_coment_post", Coment_Post.class);
    arp.addMapping("mm_like_post", Like_Post.class);
  }

  @Override
  public void configInterceptor(Interceptors me) {
    me.add(new SessionInViewInterceptor());
    me.add(new GlobalInterceptor());
  }

  @Override
  public void configHandler(Handlers me) {
    me.add(new ContextPathHandler(getProperty("CONTEXT_PATH")));
  }

  public static void main(String[] args) {
    JFinal.start("WebRoot", 8081, "/", 5);
  }
}
